package com.cwf.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cwf.common.enums.IsDeleteEnum;
import com.cwf.framework.aspectj.annotation.MyPage;
import com.cwf.system.dao.ApiManagementMapper;
import com.cwf.system.entity.ApiManagement;
import com.cwf.system.service.ApiManagementService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (ApiManagement)表服务实现类
 *
 * @author makejava
 * @since 2023-02-22 14:55:37
 */
@Service("apiManagementService")
public class ApiManagementServiceImpl extends ServiceImpl<ApiManagementMapper, ApiManagement> implements ApiManagementService {

    @Autowired
    ApiManagementService apiManagementService;

    @Override
    public boolean addApi(ApiManagement apiManagement) {

        LambdaQueryWrapper<ApiManagement> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ApiManagement::getPath,apiManagement.getPath());
        lambdaQueryWrapper.eq(ApiManagement::getRequest,apiManagement.getRequest());


        List<ApiManagement> list1 = apiManagementService.list(lambdaQueryWrapper);
        boolean empty = list1.isEmpty();
        if (!empty) {
//            lambdaQueryWrapper .eq(ApiManagement::getRequest,apiManagement.getRequest());

            return false;
//            if (list1 == null) {
//
//            }
        }

        return apiManagementService.save(apiManagement);
    }

    @MyPage
    @Override
    public PageInfo<ApiManagement> searchApi(ApiManagement apiManagement) {
        LambdaQueryWrapper<ApiManagement> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(
                apiManagement.getPath() !=null,
                ApiManagement::getPath,
                apiManagement.getPath())
                .eq(
                        apiManagement.getDes()!=null,
                        ApiManagement::getDes,
                        apiManagement.getDes()
                        )
                .eq(
                        apiManagement.getRequest()!=null,
                        ApiManagement::getRequest,
                        apiManagement.getRequest()
                )
                .eq(
                        apiManagement.getGroups() !=null,
                        ApiManagement::getGroups,
                        apiManagement.getGroups()
                ).
                eq(ApiManagement::getIsDelete,
                        IsDeleteEnum.NO.getCode());

        List<ApiManagement> apiManagementList = apiManagementService.list(lambdaQueryWrapper);

        PageInfo<ApiManagement> of = PageInfo.of(apiManagementList);

        return of;
    }

    @Override
    public Boolean deleteApi(ApiManagement apiManagement) {

        boolean b = apiManagementService.updateById(apiManagement);

        return b;
    }

    @Override
    public Object updateApi(ApiManagement build) {

        return apiManagementService.updateById(build);
    }
}
